﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Web;

namespace Uranus.Common
{
    public class CookieCommon
    {
        public string CookieName { get; set; }
        public string CookieValue { get; set; }
        public DateTime Cookietime { get; set; }

        private static string Domian = ConfigurationManager.AppSettings["Domain"];


        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="context">上下文对象</param>
        /// <param name="str">cookie集合</param>
        public static void SetCookie(HttpContextBase context, List<CookieCommon> list)
        {
            if (context != null)
            {
                foreach (CookieCommon cookieCommon in list)
                {
                    SetCookie(context, cookieCommon.CookieName, cookieCommon.CookieValue, cookieCommon.Cookietime);
                }
            }
            else
            {
                throw new Exception("上下文不存在");
            }
        }
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="context">上下文对象</param>
        /// <param name="cookiename">cookie名称</param>
        /// <param name="cookievalue">cookie内容</param>
        /// <param name="cookietime">cookie时间</param>
        public static void SetCookie(HttpContextBase context, string cookiename, string cookievalue, DateTime cookietime)
        {
            if (context != null && (context.Response.Cookies[cookiename] == null || string.IsNullOrEmpty(context.Response.Cookies[cookiename].Value)))
            {
                HttpCookie cookieName = new HttpCookie(cookiename, cookievalue);
                if (context.Response.Cookies[cookiename] != null)
                {
                    cookieName = context.Response.Cookies[cookiename];
                    cookieName.Value = cookievalue;
                    if (cookietime.Ticks != 0)
                        cookieName.Expires = cookietime;
                    if (!string.IsNullOrEmpty(Domian))
                    {
                        cookieName.Domain = Domian;
                    }
                    cookieName.Path = "/";
                }
                else
                {
                    if (cookietime.Ticks != 0)
                        cookieName.Expires = cookietime;
                    if (!string.IsNullOrEmpty(Domian))
                    {
                        cookieName.Domain = Domian;
                    }
                    cookieName.Path = "/";
                    context.Response.Cookies.Add(cookieName);
                }
            }
        }/// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="context">上下文对象</param>
        /// <param name="str">cookie集合</param>
        public static void SetCookie(HttpContext context, List<CookieCommon> list)
        {
            if (context != null)
            {
                foreach (CookieCommon cookieCommon in list)
                {
                    SetCookie(context, cookieCommon.CookieName, cookieCommon.CookieValue, cookieCommon.Cookietime);
                }
            }
            else
            {
                throw new Exception("上下文不存在");
            }
        }
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="context">上下文对象</param>
        /// <param name="cookiename">cookie名称</param>
        /// <param name="cookievalue">cookie内容</param>
        /// <param name="cookietime">cookie时间</param>
        public static void SetCookie(HttpContext context, string cookiename, string cookievalue, DateTime cookietime)
        {
            if (context != null && (context.Response.Cookies[cookiename] == null || string.IsNullOrEmpty(context.Response.Cookies[cookiename].Value)))
            {
                HttpCookie cookieName = new HttpCookie(cookiename, cookievalue);
                if (context.Response.Cookies[cookiename] != null)
                {
                    cookieName = context.Response.Cookies[cookiename];
                    cookieName.Value = cookievalue;
                    if (cookietime.Ticks != 0)
                        cookieName.Expires = cookietime;
                    if (!string.IsNullOrEmpty(Domian))
                    {
                        cookieName.Domain = Domian;
                    }
                    cookieName.Path = "/";
                }
                else
                {
                    if (cookietime.Ticks != 0)
                        cookieName.Expires = cookietime;
                    if (!string.IsNullOrEmpty(Domian))
                    {
                        cookieName.Domain = Domian;
                    }
                    cookieName.Path = "/";
                    context.Response.Cookies.Add(cookieName);
                }
            }
        }

        public static string GetCookie(HttpContextBase context, string cookiename)
        {
            if (context.Request.Cookies[cookiename] != null)
            {
                return context.Request.Cookies[cookiename].Value;
            }
            return string.Empty;
        }
    }
}
